#!/usr/bin/python
######
#       guiadapter.py
#       Copyright Etelos Inc. 2007
#       Darien Hager
#       darien@etelos.com
######
import threading
class guiadapter:
	
	FILE_DEL = 0
	FILE_MOD = 1	
	
	def __init__(self,coord):
		self.__mlock = threading.RLock()		
		self.__filemessages = {}
		self.__clock = threading.RLock()		
		self.__choicemessages = {}
		self.__parent = coord
	def tuplizePathbits(self,pb):
		""" Return a sanitized version of the given pathbits representation. 
		
		For example, a string becomes a one-item tuple, a list becomes a tuple, 
		and the contents of any resulting tuple are *unicode* strings. """
		
		temp = [] # Store vals into this
		
		# Put everything in list as appropriate.
		if(type(pb) == tuple):
			temp = list(pb)
		elif(type(pb) == list):
			temp = pb
		elif((type(pb) == str) or (type(pb) == unicode)):
			temp = [pb,]
		elif(type(pb) == int):
			temp = [str(pb),]
		else:
			assert False
			
		# Iterate list and fix individual items to unicode
		for i in range(0,len(temp)):
			if(type(temp[i]) != unicode):
				temp[i] = unicode(temp[i])
			
		# Done fixing stuff up? Back to a tuple we go!		
		return tuple(temp) 
		
	def getMessages(self):
		self.__mlock.acquire()
		ret = self.__filemessages
		self.__filemessages = {}		
		self.__mlock.release()	
		return ret
	def setMessageResponse(self,pathbits,choice):
		pathbits = self.tuplizePathbits(pathbits)
		self.__clock.acquire()
		self.__choicemessages[pathbits] = choice
		self.__clock.release()		
	def processResponses(self):
		self.__clock.acquire()
		for pb in self.__choicemessages.keys():
			response = self.__choicemessages[pb]
			if(response == "download"):
				self.__parent.addFileSet(pb)
			elif(response == "upload"):
				self.__parent.forceUpload(pb)
			elif(response == "unsub"):
				self.__parent.unsub(pb)
			else:
				assert False, "Bad return value for user-chosen action from GUI: "+response		
		self.__choicemessages = {}
		self.__clock.release()
		return
	
	def addFileMessage(self,pathbits,type,details = None):
		if(details == None): details = {}
		pathbits = self.tuplizePathbits(pathbits)
		if(type == self.FILE_DEL):
			choices = {}
			choices["Re-download the server's copy"] = "download"
			choices["Unsubscribe this file"] = "unsub"
			message = "A file was deleted locally."				
			self.__mlock.acquire()
			self.__filemessages[pathbits] = (message,choices)
			self.__mlock.release()
			return			
					
		elif(type == self.FILE_MOD):
			choices = {}
			choices["Use the server's copy, overwrite the local file"] = "download"
			choices["Use the local copy, overwrite the server's file"] = "upload"
			choices["Unsubscribe this file"] = "unsub"
			if("aheadby" in details.keys()):
				message = "A file was modified locally but the server claims to have a newer copy by "+str(details['aheadby'])+" seconds."
			else:
				message = "A file was modified locally but the server claims to have a newer copy."
			self.__mlock.acquire()
			self.__filemessages[pathbits] = (message,choices)
			self.__mlock.release()
		else:
			assert False, "Type "+str(type)+"not recognized."
			